package com.example.smart_city_0829.activity.pet_hos;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.petHos.PetType;
import com.example.smart_city_0829.entity.petHos.PubInquiryList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeGv;
import com.example.smart_city_0829.view.MaxSizeLv;

import java.util.List;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Response;

public class PetHospitalActivity extends BaseActivity {

    private MaxSizeGv petTypeGv;
    private MaxSizeLv myTreatment;
    private MaxSizeLv treatmentCasesLv;

    private int allInquiryPageNum = 1;
    private int myInquiryPageNum = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pet_hospital;
    }

    @Override
    protected void initView() {
        petTypeGv = (MaxSizeGv) findViewById(R.id.pet_type_gv);
        myTreatment = (MaxSizeLv) findViewById(R.id.my_treatment);
        treatmentCasesLv = (MaxSizeLv) findViewById(R.id.treatment_cases_lv);
    }

    @Override
    protected void initData() {
        api.getPetType().enqueue(new NetCallBack<PetType>() {
            @Override
            public void onResponse(Call<PetType> call, Response<PetType> response) {
                if (response.body() == null) return;

                final List<PetType.DataBean> data = response.body().getData();
                petTypeGv.setAdapter(new GvAdapter(data,context));
            }
        });

        //获取宠物问诊历史列表
        api.getMyPetInguiryHistory(Constant.pageSize,myInquiryPageNum).enqueue(new NetCallBack<PubInquiryList>() {
            @Override
            public void onResponse(Call<PubInquiryList> call, Response<PubInquiryList> response) {
                if (response.body() == null) return;

                final List<PubInquiryList.RowsBean> rows = response.body().getRows();
                myTreatment.setAdapter(new PubInquiryListAdapter(rows,context));
            }
        });

        //获取公开的问诊列表
        api.getAllPubInquiryList(Constant.pageSize,allInquiryPageNum).enqueue(new NetCallBack<PubInquiryList>() {
            @Override
            public void onResponse(Call<PubInquiryList> call, Response<PubInquiryList> response) {
                if (response.body() == null) return;

                final List<PubInquiryList.RowsBean> rows = response.body().getRows();
                treatmentCasesLv.setAdapter(new PubInquiryListAdapter(rows,context));
            }
        });
    }

    @Override
    protected void initEvent() {
        //宠物分类列表点击
        petTypeGv.setOnItemClickListener((parent, view, position, id) -> {
            final PetType.DataBean item = (PetType.DataBean) parent.getAdapter().getItem(position);

            migrateTo(FindDoctorActivity.class);
        });

        //我的问诊列表点击
        myTreatment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final PubInquiryList.RowsBean item = (PubInquiryList.RowsBean) parent.getAdapter().getItem(position);

                final Intent intent = new Intent(context, InterrogationDetailActivity.class);
                intent.putExtra("inquiryData",item);
                startActivity(intent);
            }
        });


        //公开问诊列表点击
        treatmentCasesLv.setOnItemClickListener((parent, view, position, id) -> {
            final PubInquiryList.RowsBean item = (PubInquiryList.RowsBean) parent.getAdapter().getItem(position);
            //TODO 午休
            final Intent intent = new Intent(context, InterrogationDetailActivity.class);
            intent.putExtra("inquiryData",item);
            intent.putExtra("type",1);
            startActivity(intent);
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "宠物医院";
    }

    //宠物分类宫格列表适配器
    static class GvAdapter extends CommonAdapter<PetType.DataBean,CommonAdapter.CommGridViewHolder>{

        public GvAdapter(List<PetType.DataBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(PetType.DataBean data, int position, CommGridViewHolder viewHolder, View convertView, Context context) {
            Glide.with(context)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .transform(new RoundedCorners(8),new RoundedCorners(8))
                    .into(viewHolder.im);

            viewHolder.serverName.setText(String.valueOf(data.getName()));
        }

        @Override
        protected CommGridViewHolder getViewHolder() {
            return new CommGridViewHolder();
        }

        @Override
        protected void initView(CommGridViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.serverName = (TextView) convertView.findViewById(R.id.server_name);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.server_list_item;
        }
    }

    //公开问诊列表适配器
    static class PubInquiryListAdapter extends CommonAdapter<PubInquiryList.RowsBean,CommonAdapter.CommListViewHolder>{

        public PubInquiryListAdapter(List<PubInquiryList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(PubInquiryList.RowsBean data, int position, CommListViewHolder viewHolder, View convertView, Context context) {
            if (data.getDoctor() != null){
                viewHolder.title.setText(String.valueOf(data.getDoctor().getName()));
                viewHolder.content.setText("案例描述："+ data.getQuestion());
                Glide.with(context)
                        .load(Constant.BASE_URL + data.getDoctor().getAvatar())
                        .transform(new RoundedCorners(8),new RoundedCorners(8))
                        .into(viewHolder.im);
            }else {
                viewHolder.title.setText("暂未查询到医生信息");
                viewHolder.content.setText("案例描述："+ data.getQuestion());
            }
        }

        @Override
        protected CommListViewHolder getViewHolder() {
            return new CommListViewHolder();
        }

        @Override
        protected void initView(CommListViewHolder viewHolder, View convertView) {
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.common_list_item2;
        }
    }
}